package br.unifor.ads.pjappfinanceiro_dkp.ui;

import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import br.unifor.ads.pjappfinanceiro.business.UsuarioBussiness;
import br.unifor.ads.pjappfinanceiro.entity.Usuario;
import br.unifor.ads.pjappfinanceiro.exceptions.BusinessException;
import br.unifor.ads.pjappfinanceiro_dkp.manager.UsuarioManager;
import br.unifor.ads.pjappfinanceiro_dkp.utils.Context;

public class FrmCadUsuario extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1961223898020724093L;
	private JTextField edtNome;
	private JTextField edtLogin;
	private JPasswordField edtPassword;
	private JTextField edtBuscar;
	private UsuarioBussiness usuarioBusiness = new UsuarioBussiness();
	private UsuarioManager usuarioManager = new UsuarioManager();
	private JTextField edtId;

	/**
	 * Create the panel.
	 */
	public FrmCadUsuario() {
		Context.getContext().setTitle("Cadastro de Usuários");
		setBorder(new TitledBorder(new TitledBorder(
				UIManager.getBorder("TitledBorder.border"), "",
				TitledBorder.LEADING, TitledBorder.TOP, null, null), "",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(null);

		JButton btnInserir = new JButton("Inserir");
		btnInserir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Usuario usuario = new Usuario();
				usuario.setLogin(edtLogin.getText());
				usuario.setNome(edtNome.getText());
				usuario.setSenha(String.valueOf(edtPassword.getPassword()));
				try {
					// usuarioBusiness.cadastrarUsuario(usuario);
					JOptionPane.showMessageDialog(null,
							usuarioManager.inserirUsuario(usuario));
				} catch (BusinessException e1) {

					e1.printStackTrace();
				}
			}
		});
		btnInserir.setBounds(10, 11, 89, 23);
		add(btnInserir);

		JButton btnAlterar = new JButton("Alterar");
		btnAlterar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if (edtId.getText() != null) {
					Usuario usuario = new Usuario();
					usuario.setId(Integer.parseInt(edtId.getText()));
					usuario.setNome(edtNome.getText());
					usuario.setLogin(edtLogin.getText());
					usuario.setSenha(String.valueOf(edtPassword.getPassword()));
					try {
						JOptionPane.showMessageDialog(null,
								usuarioManager.updateUsuario(usuario));
					} catch (HeadlessException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (BusinessException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}

			}
		});
		btnAlterar.setBounds(102, 11, 89, 23);
		add(btnAlterar);

		JButton btnExcluir = new JButton("Excluir");
		btnExcluir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {

					JOptionPane.showMessageDialog(null, usuarioManager
							.deleteUsuario(Integer.parseInt(edtId.getText())));
					
					limparTela();
				} catch (NumberFormatException e1) {
					e1.printStackTrace();
				} catch (BusinessException e1) {
					e1.printStackTrace();
				}

			}
		});
		btnExcluir.setBounds(195, 11, 89, 23);
		add(btnExcluir);

		JLabel lblNewLabel = new JLabel("Nome:");
		lblNewLabel.setBounds(25, 62, 46, 14);
		add(lblNewLabel);

		edtNome = new JTextField();
		edtNome.setBounds(25, 78, 348, 20);
		add(edtNome);
		edtNome.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("Login:");
		lblNewLabel_1.setBounds(25, 111, 37, 14);
		add(lblNewLabel_1);

		edtLogin = new JTextField();
		edtLogin.setBounds(25, 128, 348, 20);
		add(edtLogin);
		edtLogin.setColumns(10);

		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setBounds(25, 165, 46, 14);
		add(lblSenha);

		edtPassword = new JPasswordField();
		edtPassword.setBounds(25, 181, 348, 20);
		add(edtPassword);

		JButton btnSair = new JButton("Home");
		btnSair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
                   FrmPrincipal frame = Context.getContext();
                   JPanel panel = new JPanel();
                   frame.setContentPane(panel);
                   frame.montaMenu(panel);
                   frame.setVisible(true);
			}
		});
		btnSair.setBounds(284, 11, 89, 23);
		add(btnSair);

		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 4));
		panel.setBounds(25, 231, 478, 102);
		add(panel);
		panel.setLayout(null);

		JLabel lblNewLabel_2 = new JLabel("Login:");
		lblNewLabel_2.setBounds(56, 35, 73, 14);
		panel.add(lblNewLabel_2);

		edtBuscar = new JTextField();
		edtBuscar.setBounds(97, 32, 251, 20);
		panel.add(edtBuscar);
		edtBuscar.setColumns(10);

		JButton btnBuscar = new JButton("Buscar");
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				Usuario usuario = new Usuario();
				try {
					usuario = usuarioBusiness.localizarUsuarioPorLogin(edtBuscar.getText());
					
					edtId.setText(String.valueOf(usuario.getId()));
					edtNome.setText(usuario.getNome());
					edtLogin.setText(usuario.getLogin());
				} catch (BusinessException e) {

					e.printStackTrace();
				}

			}
		});
		btnBuscar.setBounds(358, 31, 89, 23);
		panel.add(btnBuscar);

		edtId = new JTextField();
		edtId.setBounds(25, 42, 46, 20);
		edtId.setVisible(false);
		add(edtId);
		edtId.setColumns(10);

	}
	
	
	public void limparTela(){
		edtId.setText("");
		edtNome.setText("");
		edtLogin.setText("");
		edtPassword.setText("");
		edtBuscar.setText("");
	}
	
	
}
